package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.wx.*;
import com.cskaoyan.service.wx.WxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("wx/order/")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @RequestMapping("submit")
    public BaseRespVo wxOrderSubmit(@RequestBody WxOrderSubmitBO wxOrderSubmitBO){
        OrderIdVO orderId = wxOrderService.orderSubmit(wxOrderSubmitBO);
        return BaseRespVo.ok(orderId);
    }

    @RequestMapping("list")
    public BaseRespVo wxOrderList(Integer showType, Integer page, Integer size){
//        if(showType == null){
//            return BaseRespVo.fail("showType = null 参数错误");
//        }
        WxOrderListBeanVO wxOrderListBeanVO = wxOrderService.queryWxOrderList(showType,page,size);
        return BaseRespVo.ok(wxOrderListBeanVO);
    }

    @RequestMapping("detail")
    public BaseRespVo wxOrderDetail(Integer orderId){
        WxOrderDetailVO wxOrderDetailVO = wxOrderService.queryWxOrderDetail(orderId);
        return BaseRespVo.ok(wxOrderDetailVO);
    }

    @RequestMapping("cancel")
    public BaseRespVo wxOrderCancel(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        try {
            wxOrderService.deleteWxOrder(orderId);
        }catch (Exception e){
            return BaseRespVo.ok("订单删除失败");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("prepay")
    public BaseRespVo wxOrderPrePay(@RequestBody Map<String ,Integer> map){
        Integer orderId = map.get("orderId");
        try {
            wxOrderService.prepayWxOrder(orderId);
        }catch (Exception e){
            return BaseRespVo.ok("订单支付失败");
        }
        return BaseRespVo.ok(null,"订单支付成功");
    }

    @RequestMapping("refund")
    public BaseRespVo wxOrderRefund(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        try {
            wxOrderService.refundWxOrder(orderId);
        }catch (Exception e){
            return BaseRespVo.ok("订单支付失败");
        }
        return BaseRespVo.ok(null,"申请退款中");
    }

    @RequestMapping("confirm")
    public BaseRespVo wxOrderConfirm(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        try {
            wxOrderService.confirmWxOrder(orderId);
        }catch (Exception e){
            return BaseRespVo.ok("订单支付失败");
        }
        return BaseRespVo.ok(null,"用户确认收货");
    }

    @RequestMapping("delete")
    public BaseRespVo wxOrderDelete(@RequestBody Map<String, Integer> map){
        Integer orderId = map.get("orderId");
        try {
            wxOrderService.deleteWxOrder(orderId);
        }catch (Exception e){
            return BaseRespVo.ok("订单删除失败");
        }
        return BaseRespVo.ok("订单删除成功");
    }

    @RequestMapping("goods")
    public BaseRespVo wxOrderGoods(Integer orderId, Integer goodsId){
        WxOrderGoods wxOrderGoods = wxOrderService.queryOrderGoods(orderId, goodsId);
        return BaseRespVo.ok(wxOrderGoods);
    }

    @RequestMapping("comment")
    public BaseRespVo wxOrderComment(@RequestBody WxOrderCommentBO wxOrderCommentBO){
        try {
            wxOrderService.updateWxOrderComment(wxOrderCommentBO);
        }catch (Exception e){
            return BaseRespVo.ok("出错啦！");
        }
        return BaseRespVo.ok("评价成功");
    }
}
